package cn.cs.service.impl;

import cn.cs.client.RestCsImageClient;
import cn.cs.client.RestCsProductClient;
import cn.cs.common.*;
import cn.cs.pojo.CsImage;
import cn.cs.pojo.CsProduct;
import cn.cs.service.HomeService;
import cn.cs.vo.HotItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component
public class HomeServiceImpl implements HomeService {

//    //@Autowired
//    @Resource
//    private RestDmItemTypeClient restDmItemTypeClient;
//
    //@Autowired
    @Resource
    private RestCsProductClient restCsProductClient;

    //@Autowired
    @Resource
    private RestCsImageClient restCsImageClient;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 查询图片信息
     *
     * @param restCsImageClient
     * @param type
     * @param category
     * @return
     */
    public String getImageUrl(RestCsImageClient restCsImageClient, Long id, Integer type, Integer category) {
        //查询图片信息
        List<CsImage> csImageList = null;
        //从reidis中获取图片信息
        String key = Constants.IMAGE_TOKEN_PREFIX + id + "_" + type + "_" + category;
        String imgUrl = (String) redisUtils.get(key);
        //如果redis中没有,则到数据库中获取
        if (EmptyUtils.isEmpty(imgUrl)) {
            csImageList = restCsImageClient.queryCsImageList(id, type, category);
            imgUrl = csImageList.get(0).getImgUrl();
            //同步图片到redis
            redisUtils.set(key, imgUrl);
        }
        return imgUrl;
    }

//    /***
//     * 查询横向导航条
//     * @return
//     * @throws Exception
//     */
//    @Override
//    public List<DmItemType> queryTransverse() throws Exception {
//        Map<String, Object> param = new HashMap<String, Object>();
//        param.put("level", "1");
//        return restDmItemTypeClient.getDmItemTypeListByMap(param);
//    }

    /***
     * 查询导航条
     * @return
     * @throws Exception
     */
    @Override
    public List<HotItemVo> queryBanner() throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        //显示轮播图中的前5个
        //isBanner为1，表示是热推产品
        param.put("isBanner", 1);
        param.put("beginPos", 0);
        param.put("pageSize", 5);
        List<CsProduct> csProductList = restCsProductClient.getCsProductListByMap(param);
        List<HotItemVo> hotProduceVoList = new ArrayList<HotItemVo>();
        if (EmptyUtils.isEmpty(csProductList)) {
            return null;
        }

        for (CsProduct csProduct : csProductList) {
            //组装图片信息，接口返回数据
            HotItemVo hotItemVo = new HotItemVo();
            //把一样的字段的值复制过去
            BeanUtils.copyProperties(csProduct, hotItemVo);
            String imgUrl = getImageUrl(restCsImageClient, Long.parseLong(csProduct.getId()), Constants.Image.ImageType.carousel, Constants.Image.ImageCategory.item);
            hotItemVo.setImgUrl(imgUrl);
            hotProduceVoList.add(hotItemVo);
        }

        return hotProduceVoList;
    }


    /***
     * 查询当日推荐
     * @return
     * @throws Exception
     */
    @Override
    public List<HotItemVo> queryTodayRecommend() throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isRecommend", 1);
        param.put("beginPos", 0);
        param.put("pageSize", 6);
        List<CsProduct> csProductList = restCsProductClient.getCsProductListByMap(param);
        List<HotItemVo> hotProductVoList = new ArrayList<HotItemVo>();
        if (EmptyUtils.isEmpty(csProductList)) {
            return null;
        }
        for (CsProduct csProduct : csProductList) {
            HotItemVo hotItemVo = new HotItemVo();
            BeanUtils.copyProperties(csProduct, hotItemVo);
            String imgUrl = getImageUrl(restCsImageClient, Long.parseLong(csProduct.getId()), Constants.Image.ImageType.normal, Constants.Image.ImageCategory.item);
            hotItemVo.setImgUrl(imgUrl);
            hotProductVoList.add(hotItemVo);
        }
        return hotProductVoList;
    }


    /****
     * 查询热门产品
     * @return
     * @throws Exception
     */
    @Override
    public List<HotItemVo> queryHotItems(Long itemTypeId) throws Exception {
       /* List<HotItemVo> hotItemVoList = new ArrayList<HotItemVo>();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("sortDesc", "commentCount");
        param.put("itemType1Id", itemTypeId);
        param.put("beginPos", 0);
        param.put("pageSize", 10);
        List<DmItem> itemList = restDmItemClient.getDmItemListByMap(param);
        if (EmptyUtils.isEmpty(itemList)) {
            return null;
        }
        for (DmItem dmItem : itemList) {
            HotItemVo hotItemVo = new HotItemVo();
            BeanUtils.copyProperties(dmItem, hotItemVo);
            String imgUrl = getImageUrl(restCsImageClient, dmItem.getId(), Constants.Image.ImageType.normal, Constants.Image.ImageCategory.item);
            hotItemVo.setImgUrl(imgUrl);
            hotItemVoList.add(hotItemVo);
        }
        return hotItemVoList;*/
       return null;
    }

    public void addItem(CsProduct csProduct) {
       /* try {
            restDmItemClient.qdtxAddDmItem(dmItem);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }
}
